//
//  PublicTableViewCell.m
//  AAIM
//
//  Created by 远的小布丁 on 2020/1/20.
//  Copyright © 2020 AAIM. All rights reserved.
//

#import "PublicTableViewCell.h"
@interface PublicTableViewCell()<UIGestureRecognizerDelegate>
@end
@implementation PublicTableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(nonnull NSString *)reuseIdentifier withCellType:(nonnull NSString *)type{
    
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        _cellType = type;
        [self setShowUI:type];
    }
    return self;
}

- (void)setShowUI:(NSString *)type{
    if ([type isEqualToString:@"PublicListCell"]) {

    self.backgroundColor = [UIColor whiteColor];
        self.headerImg = [[UIImageView alloc]initWithFrame:CGRectMake(15, 9, 50, 50)];
        self.headerImg.layer.cornerRadius = 15;
        self.headerImg.layer.masksToBounds = YES;
    [self.contentView addSubview:self.headerImg];
        self.nameLbl = [[UILabel alloc]initWithFrame:CGRectMake(self.headerImg.right+6, 12, self.width-self.headerImg.right-21, 21)];
        self.nameLbl.font = kMainFont(15);
        self.nameLbl.text = @"钢铁侠  服务号";
    [self.contentView addSubview:self.nameLbl];
        
        self.contentLbl = [[UILabel alloc]initWithFrame:CGRectMake(self.headerImg.right+6, self.nameLbl.bottom+4, self.nameLbl.width, 17)];
       self.contentLbl.font = kMainFont(12);
        self.contentLbl.text = @"「服务号」服务你我他";
        self.contentLbl.textColor = KHEXCOLOR(@"#AFAFAF");
[self.contentView addSubview:self.contentLbl];
    }else if ([_cellType isEqualToString:@"AttentionCell"]){
       self.backgroundColor = [UIColor whiteColor];
            self.headerImg = [[UIImageView alloc]initWithFrame:CGRectMake(15, 6, 58, 58)];
        self.headerImg.layer.cornerRadius = 29;
        self.headerImg.layer.masksToBounds = YES;
        self.headerImg.image = [UIImage imageNamed:@"我的-头像"];
        self.headerImg.userInteractionEnabled = YES;
        [self.contentView addSubview:self.headerImg];
        UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(touchesPublicInfo)];
        tap.numberOfTouchesRequired = 1;
        tap.numberOfTapsRequired = 1;
        [self.headerImg addGestureRecognizer:tap];
            self.nameLbl = [[UILabel alloc]initWithFrame:CGRectMake(self.headerImg.right+15, 6, kScreenWidth-self.headerImg.right-30, 26)];
            self.nameLbl.font = kFont(18.5);
            self.nameLbl.text = @"钢铁女孩";
        [self.contentView addSubview:self.nameLbl];
            
        
            self.contentLbl = [[UILabel alloc]initWithFrame:CGRectMake(self.headerImg.right+15, self.nameLbl.bottom+4, self.nameLbl.width, 17)];
           self.contentLbl.font = kMainFont(12);
        self.contentLbl.numberOfLines = 0;
           self.contentLbl.textColor = KHEXCOLOR(@"#262B2F");
            self.contentLbl.text = @"每天分享国内外湖两旺与投资消息速递，第一每天分享国内外湖两旺与投资消息速递，第一每天分享国内外湖两旺与投资消息速递，第一每天分享国内外湖两旺与投资消息速递，第一每天分享国内外湖两旺与投资消息速递，第一每天分享国内外湖两旺与投资消息速递，第一";
        CGRect shopNameWidth = [self.contentLbl.text boundingRectWithSize:CGSizeMake(self.nameLbl.width, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName: self.contentLbl.font} context:nil];
        self.contentLbl.frame= CGRectMake(self.headerImg.right+15, self.nameLbl.bottom+4, self.nameLbl.width, shopNameWidth.size.height);
        [self.contentView addSubview:self.contentLbl];
        
        self.intoPublicBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        self.intoPublicBtn.frame = CGRectMake(self.contentLbl.left, self.contentLbl.bottom+10, self.contentLbl.width/2, 21);
        [self.intoPublicBtn setTitle:@"进入公众号" forState:UIControlStateNormal];
        [self.intoPublicBtn setTitleColor:KHEXCOLOR(@"#4C80BC") forState:UIControlStateNormal];
        self.intoPublicBtn.titleLabel.font = kFont(15);
        [self.intoPublicBtn setTitleEdgeInsets:UIEdgeInsetsMake(0, -self.contentLbl.width/8, 0, self.contentLbl.width/8)];
        self.intoPublicBtn.tag = 1001;
        [self.intoPublicBtn addTarget:self action:@selector(intoPublicPageActive:) forControlEvents:UIControlEventTouchUpInside];
        [self.contentView addSubview:self.intoPublicBtn];
        
        self.attentionBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        self.attentionBtn.frame = CGRectMake(self.intoPublicBtn.right, self.contentLbl.bottom+10, self.contentLbl.width/2, 21);
        [self.attentionBtn setTitle:@"关注" forState:UIControlStateNormal];
        [self.attentionBtn setTitle:@"不再关注" forState:UIControlStateSelected];
        self.attentionBtn.tag = 1002;
        [self.attentionBtn setTitleColor:KHEXCOLOR(@"#4C80BC") forState:UIControlStateNormal];
        self.attentionBtn.titleLabel.font = kFont(15);
       
        [self.attentionBtn addTarget:self action:@selector(attentionPublicPageActive:) forControlEvents:UIControlEventTouchUpInside];
        [self.contentView addSubview:self.attentionBtn];
    }
}

- (void)touchesPublicInfo{
    if ([self.delegate respondsToSelector:@selector(touchesToPublicInfoPage)]) {
        [self.delegate touchesToPublicInfoPage];
    }
}

- (void)intoPublicPageActive:(UIButton *)sender{
    if (self.delegate) {
        [self.delegate intoAttentionPageDelegate:sender];
    }
}

- (void)attentionPublicPageActive:(UIButton *)sender{
    if (self.delegate) {
           [self.delegate intoAttentionPageDelegate:sender];
       }
}


-(void)setIndexPath:(NSIndexPath *)indexPath{
    _indexPath = indexPath;
}

- (void)setModel:(PublicModel *)model{

    if ([_cellType isEqualToString:@"PublicListCell"]) {

    if (_indexPath.row%2 == 1) {
        _nameLbl.textColor = KHEXCOLOR(@"#D0021B");
        _headerImg.image = [UIImage imageNamed:@"公众号"];
    }else{
        _nameLbl.textColor = KHEXCOLOR(@"#419A59");
        _headerImg.image = [UIImage imageNamed:@"服务号"];
    }
        _nameLbl.text = model.title;
        KWeakSelf;
        [_headerImg sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",userHeaderImage,model.head]] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
            weakSelf.headerImg.image = image;
        }];
        _contentLbl.text = model.describe;
    }else if ([_cellType isEqualToString:@"AttentionCell"]){
        _nameLbl.text = model.title;
             KWeakSelf;
             [_headerImg sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",userHeaderImage,model.head]] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
                 weakSelf.headerImg.image = image;
             }];
             _contentLbl.text = model.describe;
        self.attentionBtn.selected = [model.isadd integerValue];
    }
}

- (CGFloat)calculationCellHeight:(NSString *)content{
    CGRect rect = [content boundingRectWithSize:CGSizeMake(kScreenWidth - 88, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName: self.textLabel.font} context:nil];
    return rect.size.height + 80.f;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}


@end
